package com.yao.component.pictureselector.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yao.component.pictureselector.CommonUtil;
import com.yao.component.pictureselector.PhotoBrowseActivity;
import com.yao.component.pictureselector.R;
import com.yao.component.pictureselector.widget.AlertMsgDialog;
import com.yao.component.pictureselector.widget.ViewHolder;

 public class ImgGrideAdapter extends CommonAdapter<String>
{


	private onPictureClickListener mListener;
	private int diaplayWidth;
	private boolean isSingle;
	public ImgGrideAdapter(Context context, List<String> mDatas, int itemLayoutId, boolean isSingle)
	{
		super(context, mDatas, itemLayoutId);
		this.isSingle=isSingle;
		diaplayWidth= (CommonUtil.getDisplayWidth(context))/4;
//		this.mDirPath = dirPath;
	}
	public void setOnPictureClickListener(onPictureClickListener listener){
		this.mListener=listener;
	}
	@Override
	public void convert(final ViewHolder helper, final String item)
	{
		//设置no_pic
		helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
		//设置no_selected
//				helper.setImageResource(R.id.id_item_select,
//						R.drawable.picture_unselected);
		//设置图片
		helper.setImageByUrl(R.id.id_item_image, item);
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		if (isSingle){
			mSelect.setVisibility(View.GONE);
		}else {
			mSelect.setVisibility(View.VISIBLE);
		}
		mImageView.setLayoutParams(new RelativeLayout.LayoutParams(diaplayWidth,diaplayWidth));
		mImageView.setColorFilter(null);
		//设置ImageView的点击事件
		mSelect.setOnClickListener(new OnClickListener()
		{
			//选择，则将图片变暗，反之则反之
			@Override
			public void onClick(View v)
			{

				// 已经选择过该图片
				if (PhotoBrowseActivity.mSelectedImage.contains( item))
				{

					mSelect.setBackgroundResource(R.drawable.circular_uncheck);
					if (mListener!=null){
						mListener.onRemovePicture(item);
					}
				} else
				// 未选择该图片
				{
					if (PhotoBrowseActivity.mSelectedImage.size()>=PhotoBrowseActivity.maxSelectCount){
						AlertMsgDialog dialog=new AlertMsgDialog(mContext,"你最多只能选择"+PhotoBrowseActivity.maxSelectCount+"张照片","确定",null);
						dialog.show();
					}else {
						mSelect.setBackgroundResource(R.drawable.circular_check);
						if (mListener!=null){
							mListener.onAddPicture(item);
						}
					}

				}

			}
		});
		
		/**
		 * 已经选择过的图片，显示出选择过的效果
		 */
		if (PhotoBrowseActivity.mSelectedImage.contains(item))
		{
			mSelect.setBackgroundResource(R.drawable.circular_check);
		}else {
			mSelect.setBackgroundResource(R.drawable.circular_uncheck);
		}

	}
	public interface onPictureClickListener{
		public void onAddPicture(String path);
		public void onRemovePicture(String path);
	}
}
